<?php

namespace app\admin\controller;


class Admin extends Base
{
    // 管理员列表
    public function adminlist()
    {
        $admins = model('Admin')->order(['is_super' => 'desc', 'status' => 'desc'])->paginate(10);
        $this->assign('admins', $admins);
        return view();
    }


    // 管理员添加
    public function adminadd()
    {
        if (request()->isAjax()){
            $data = [
                'username' => input('post.username'),
                'password' => input('post.password'),
                'nickname' => input('post.nickname'),
                'email' => input('post.email'),
            ];
            $result = model('Admin')->adminadd($data);
            if ($result == 1){
                $this->success('管理员添加成功', 'admin/admin/adminlist');
            }else {
                $this->error($result);
            }
        }

        // get请求
        return view();
    }

    // 禁用
    public function disableadmin()
    {
        $admin_info = model('Admin')->find(input('post.id'));
        if (!$admin_info){
            abort('404', '用户不存在');
        }
        $admin_info->status = 0;
        $result = $admin_info->save();
        if ($result){
            $this->success('用户已禁用');
        }else{
            $this->error('操作失败');
        }
    }

    // 启用
    public function useadmin()
    {
        $admin_info = model('Admin')->find(input('post.id'));
        if (!$admin_info){
            abort('404', '用户不存在');
        }
        $admin_info->status = 1;
        $result = $admin_info->save();
        if ($result){
            $this->success('用户已启用');
        }else{
            $this->error('操作失败');
        }
    }

    // 授权
    public function grantauthority()
    {
        $admin_info = model('Admin')->find(input('post.id'));
        if (!$admin_info){
            abort('404', '用户不存在');
        }
        $admin_info->is_super = 1;
        $result = $admin_info->save();
        if ($result){
            $this->success('用户已授权');
        }else{
            $this->error('操作失败');
        }
    }


    // 取消授权
    public function cancelGrantAuthority()
    {
        $admin_info = model('Admin')->find(input('post.id'));
        if (!$admin_info){
            abort('404', '用户不存在');
        }
        $admin_info->is_super = 0;
        $result = $admin_info->save();
        if ($result){
            $this->success('已取消授权');
        }else{
            $this->error('操作失败');
        }
    }


    // 管理员编辑
    public function adminedit()
    {
        if (request()->isAjax()){
            $data = [
                'id' => input('post.id'),
                'username' => input('post.username'),
                'nickname' => input('post.nickname'),
                'email' => input('post.email'),
            ];
            $result = model('Admin')->adminedit($data);
            if ($result == 1){
                $this->success('管理员信息修改成功', 'admin/admin/adminlist');
            }else{
                $this->error($result);
            }
        }

        $admin_info = model('Admin')->find(input('id'));
        $this->assign('adminInfo', $admin_info);
        return view();
    }

    // 重置密码
    public function resetpwd()
    {
        $admin_info = model('Admin')->find(input('post.id'));
        if (!$admin_info){
            $this->error('用户不存在');
        }
        $admin_info->password = md5('123456');
        $result = $admin_info->save();
        if ($result){
            $this->success('重置密码成功');
        }else{
            $this->error('重置密码失败');
        }
    }


    // 管理员删除
    public function admindel()
    {
        $admin_info = model('Admin')->find(input('post.id'));
        if (!$admin_info){
            abort('404', '用户不存在');
        }
        $result = $admin_info->delete();
        if ($result){
            $this->success('用户已删除', 'admin/admin/adminlist');
        }else{
            $this->error('操作失败');
        }
    }

}
